﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Orion.Models;
using Orion.MyLib;

namespace Orion.Controllers
{ 
    public class WorkPageContentController : Controller
    {
        NewsArticleRepositry m_rep = new NewsArticleRepositry( );
        private IUserManager m_userMgr = new UserManager( );
        HomeIndexViewModel m_helpAbout = new HomeIndexViewModel();

        [HttpGet]
        public ActionResult CreateHelp( )
        {
            var model = new NewsArticle( );
            model.ContentType = 4;
            return View( model );
        }

        [ValidateInput( false )]
        [HttpPost]
        public ActionResult CreateHelp( NewsArticle co )
        {
            try
            {
                //Validation
                if( String.IsNullOrEmpty( co.Title ) )
                {
                    ModelState.AddModelError("Title", "Það verður að tilgreina titil");
                    return View( co );
                }

                if( String.IsNullOrEmpty( co.TextBody ) )
                {
                    ModelState.AddModelError( "Meginmál", "Það verður að tilgreina meginmál" );
                    return View( co );
                }

                co.Author = m_userMgr.CurrentUser;
                co.ContentDate = DateTime.Now;
                co.ContentType = 4;  //id for help
                co.Valid = true;
                m_rep.AddNewsArticle( co ); //Dont let the name fool you :)

                return RedirectToAction( "Index", "Work" );
            }
            catch ( Exception ex )
            {
			    var log = new Logger( );
			    log.LogExcepion( ex );
			    return View ( "Error" );
            }
        }


        [HttpGet]
        public ActionResult CreateAbout( )
        {
            var model = new NewsArticle();
            model.ContentType = 3;
            return View( model );
        }

        [ValidateInput(false)]
        public ActionResult CreateAbout( NewsArticle co )
        {
            try
            {
                //Validation
                if (String.IsNullOrEmpty( co.Title ))
                {
                    ModelState.AddModelError("Title", "Það verður að tilgreina titil");
                    return View(co);
                }

                if (String.IsNullOrEmpty( co.TextBody ))
                {
                    ModelState.AddModelError( "Meginmál", "Það verður að tilgreina meginmál" );
                    return View( co );
                }

                co.Author = m_userMgr.CurrentUser;
                co.ContentDate = DateTime.Now;
                co.ContentType = 3;  //id for about
                co.Valid = true;
                m_rep.AddNewsArticle( co ); //Dont let the name fool you... ;)

                return RedirectToAction("Index", "Work" );
            }
            catch ( Exception ex )
            {
                var log = new Logger();
                log.LogExcepion(ex);
                return View("Error");
            }
        }



        [HttpGet]
        public ActionResult EditHelp( int? id )
        {
            //validation
            if (!id.HasValue)
            {
                return View("NotFound");
            }
            var model = m_rep.GetNewsArticleById(id.Value);


            return View( model );
        }


        [ValidateInput( false )]
        [HttpPost]
        public ActionResult EditHelp( FormCollection form )
        {
            try
            {
				var na = new NewsArticle();
				UpdateModel(na);

				//validation
				if (!ModelState.IsValid)
				{
					return View(na);
				}

				//ID is returned as string, for some reason need to change it back to int for use.
				int temp;
				if (!int.TryParse(form["ID"], out temp))
				{
					throw new Exception("ID af auglýsingu tapaðist við tilraun til þess að breyta henni");
				}
				na.ID = temp;

                m_rep.UpdateNewsArticle( na );
                //1 is id on news

                return RedirectToAction( "Index", "Work" );
            }
            catch ( Exception ex )
            {
                var log = new Logger();
                log.LogExcepion(ex);
                return View("Error");
            }

        }

        [HttpGet]
        public ActionResult EditAbout( int? id )
        {
            //validation
            if (!id.HasValue)
            {
                return View( "NotFound" );
            }
            var model = m_rep.GetNewsArticleById( id.Value );


            return View( model );
        }


        [ValidateInput(false)]
        [HttpPost]
        public ActionResult EditAbout( FormCollection form )
        {
            try
            {
				var na = new NewsArticle();
				UpdateModel(na);

                //validation
                if (!ModelState.IsValid)
                {
                    return View(na);
                }

				//ID is returned as string, for some reason need to change it back to int for use.
				int temp;
				if (!int.TryParse(form["ID"], out temp))
				{
					throw new Exception("ID af auglýsingu tapaðist við tilraun til þess að breyta henni");
				}
				na.ID = temp;

                m_rep.UpdateNewsArticle(na);
                //1 is id on news

                return RedirectToAction( "Index", "Work" );
            }
            catch (Exception ex)
            {
                var log = new Logger();
                log.LogExcepion(ex);
                return View("Error");
            }

        }

        public ActionResult ViewAbout( )
        {
            m_helpAbout.NewsArticle = from ab in m_rep.GetAllNewsArticles()
                                    where ab.ContentType == 3
                                    select ab;

            return View( m_helpAbout );
        }

        public ActionResult ViewHelp()
        {
            m_helpAbout.NewsArticle = from hp in m_rep.GetAllNewsArticles()
                                    where hp.ContentType == 4
                                    select hp;

            return View( m_helpAbout );
        }

        public ActionResult GetContentById( int? id )
        {
            if ( !id.HasValue )
            {
                return View( "Error" );
            }

            var model = m_rep.GetNewsArticleById(id.Value);

            if ( model.ContentType != 3 && model.ContentType != 4)
            {
                return View( "Error" );
            }
            
            return View(model);
        }

        public ActionResult DisablePageContent( int? id )
        {
            try
        	{
				//validation
				if (!id.HasValue)
				{
					return View( "Error" );
				}
				var content = m_rep.GetNewsArticleById( id.Value );
				if( content != null )
				{
					String strUser = User.Identity.Name;
					var referrer = HttpContext.Request.UrlReferrer;
					m_rep.DisableNewsArticle( content, strUser );

					if ( referrer != null )
					{
						return Redirect(referrer.ToString());
					}
				}
				return View( );
        	}
        	catch ( Exception ex )
        	{
				var log = new Logger() ;
				log.LogExcepion( ex );
				return View ("Error" );
        	}
        }
        
        public ActionResult EnablePageContent(int? id)
        {
            try
        	{
				//validation
				if (!id.HasValue)
				{
					return View( "Error" );
				}

				var content = m_rep.GetNewsArticleById(id.Value);

                if ( content != null )
				{
					String strUser = User.Identity.Name;
					var referrer = HttpContext.Request.UrlReferrer;
                    m_rep.EnableNewsArticle( content, strUser );

					if ( referrer != null )
					{
						return Redirect( referrer.ToString( ) );
					}
				}

				return View( );    
        	}
        	catch ( Exception ex )
        	{
				var log = new Logger( );
				log.LogExcepion( ex );
				return View( "Error" );
        	}

        }

    }
}
